import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.imageio.ImageIO;
//draw map image and write to the FMap.txt and print selected point

public class CopyOfMultipleWalks {
	static ImageProcessing imp = new ImageProcessing(3);
	static BufferedImage image;
	static RandomWalkSquare[] walks;
	
	
	public CopyOfMultipleWalks(){}
	public static void main(String[] args) throws IOException{
		
		Color colorToFind = new Color(254,0,0);	
		Vector<Point> pixels = imp.pixelSearch(colorToFind,imp.readImage());	
		int[][] maps = imp.convertToMap();
		int[][] FMap = new int[maps.length][maps[0].length];
		Random rand = new Random();
		walks = new RandomWalkSquare[10];
		int numPeople = walks.length;
		int numSteps;
		//writeCSV(maps);
		
		Point[] points = new Point[numPeople];	
		for(int i=0;i<numPeople;i++){
			points[i] = generateStartPoint(pixels);
		}
		
		for(int i=0;i<numPeople;i++){
			numSteps = rand.nextInt(100)+50;//50-149
			walks[i]= new RandomWalkSquare(10,numSteps,25);
		}
		
		Person[] walkers = new Person[numPeople];
		for(int i=0;i<numPeople;i++){
			walkers[i] = new Person(points[i].x,points[i].y,10,i);
		}
		
		Point[][] paths = new Point[numPeople][150]; 		
		for(int i=0;i<numPeople;i++){
			paths[i]=walks[i].walkRandomly(walkers[i],maps);
		}
		exportToOneFile(paths);
		image=imp.image;
		writeImage(image,maps,paths);
		
		for(int i=0;i<numPeople;i++){
			addToFMap(paths[i],FMap);
		}
		
		//drawFMap(FMap);
		selectPoint(FMap);
	
    }
	
	public static void exportToOneFile(Point[][] paths) throws IOException{
		String path = "C:/Users/mengdi/Desktop/material/";
		File file = new File( path +"All"+".txt");  
    	FileWriter writer = new FileWriter(file);
    	BufferedReader br;
    	for(int i=0;i<paths.length;i++){
    		System.out.println("collecting data from "+(i+1)+".txt ...");
    		br = new BufferedReader(new FileReader(path+(i+1)+".txt"));
    		for(int j=0;j<paths[i].length;j++){
    			StringBuffer str = new StringBuffer(br.readLine());
    			writer.append("Object "+(i+1)+","+ str+"\n");
		    }
    	}
    	writer.flush();
    	writer.close();
	}
	public static Point generateStartPoint(Vector<Point> pixels){
		int size = pixels.size();
		Random generator = new Random();
		int index = generator.nextInt(size);
		return pixels.get(index);
		
	}
	public static void writeCSV(int[][] maps) throws IOException{
		File file = new File( "C:/Users/mengdi/Desktop/material/2.csv");  
        FileWriter writer = new FileWriter(file);
        for(int i=0;i<maps.length;i++){
        	for(int j=0;j<maps[0].length;j++){
        		if(maps[i][j]==1) writer.append(i+","+j+"\n");
        	}
        }
        writer.flush();
        writer.close();
	}
	public static void selectPoint(int[][] FMap){
		for(int i=0;i<FMap.length;i++){
			for(int j=0;j<FMap[0].length;j++){
				if(FMap[i][j]!=0) 
					System.out.println("("+i+","+j+")"+" Frequency:"+FMap[i][j]);
			}
		}
	}
	public static void addToFMap(Point[] path, int[][] FMap){
		int size = path.length;
		for(int i= 0;i<size;i++){
			int x = path[i].x;
			int y = path[i].y;
			FMap[x][y]++;
		}
		
	}
	public static void drawFMap(int[][] FMap) throws IOException{
		File file = new File( "C:/Users/mengdi/Desktop/material/Fmap.txt");  
        //if ( !file.exists() ) file.createNewFile();
    	FileWriter writer = new FileWriter(file);
    	for(int i=0;i<FMap.length;i++){
			for(int j=0;j<FMap[i].length;j++){
				writer.append(FMap[i][j]+" ");
			}
			writer.append("\n");
		}
    	    writer.flush();
    	    writer.close();
	}
	public static BufferedImage setImage(BufferedImage image, int[][] map){
		int w = image.getWidth();
        int h = image.getHeight(); 
		BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Color red = new Color(254,0,0);
		int redColor = red.getRGB();
		for(int i=0;i<map.length;i++){
			for(int j=0;j<map[0].length;j++){
				if(map[i][j]==1) bufferedImage.setRGB(i, j, redColor);
			}
		}
		return bufferedImage;
	}
	public static BufferedImage setPath(BufferedImage image,Point[] path, int pathColor){
		for(int i=0;i<path.length;i++){
			int x=path[i].x;
			int y=path[i].y;
			for(int m=0;m<10;m++){
				for(int n=0;n<10;n++){
					image.setRGB(x+m,y+n,pathColor);
				}
			}
		}
		return image;
	}
	public static void writeImage(BufferedImage image, int[][] map, Point[][] paths) throws IOException{
		BufferedImage bufferedImage = setImage(image,map);
		Random rand = new Random();
		Color[] colors = new Color[walks.length];
		for(int i=0;i<colors.length;i++){
			colors[i] = new Color(rand.nextFloat(),rand.nextFloat(),rand.nextFloat());
		}
		for(int i=0;i<colors.length;i++){
			bufferedImage = setPath(bufferedImage, paths[i],colors[i].getRGB());
		}		
		
		String filename = "1 walks";
		File file = new File("C:/Users/mengdi/Desktop/material/"+filename+".jpg");
		
		ImageIO.write(bufferedImage,"jpg",file);
		System.out.println(file.getName());
				
	}

}
